<template>
  <div>
    <el-row type="flex" class="row-bg" justify="center">
      <el-col :span="6"></el-col>
      <el-col :span="7">
        <!--logo区域-->
        <!--在页面直接使用组件svg-icon，其中incoClass命名等于svg文件的名称-->
        <div style="text-align: center">
          <svg-icon icon-class="cloudNoteN" style="width:200px; height: 130px"/>
        </div>
        <!--登录区域-->
        <el-card>
          <el-tabs v-model="activeName" @tab-click="handleClick">
            <el-tab-pane label="验证码登录" name="1">
              <el-form :model="userForm" status-icon :rules="userFormRules" ref="userFormRef" label-width="0px">
                <el-form-item prop="mobile">
                  <el-input v-model="userForm.mobile" placeholder="手机号"></el-input>
                </el-form-item>
                <el-form-item prop="verificationCode">
                  <el-input class="verify" v-model="userForm.verificationCode" placeholder="手机验证码"></el-input>
                  <el-button type="primary" style="margin-left: 22px;">获取验证码</el-button>
                </el-form-item>
                <el-form-item style="text-align: center">
                  <el-button class="btn" type="primary" @click="submitForm('ruleForm')">立即登录</el-button>
                </el-form-item>
              </el-form>
              <div class="register-bar">
                <el-link type="primary" :underline="false" href="https://element.eleme.io" target="_blank">遇到问题？
                </el-link>
                <router-link :to="{ name: 'register'}" style='font-size: 14px;color: #2491f6;'>
                  立即注册
                </router-link>
              </div>
              <div class="tip">
                <p>未注册的手机验证后将自动登录。注册/登录即代表您同意并遵守
                  <a href="">《云N记通行证用户服务协议》</a>
                  <a href="">《云N记通行证用户个人信息及隐私保护政策》</a>
                </p>
              </div>
            </el-tab-pane>
            <el-tab-pane label="云N记" name="0" disabled>
            </el-tab-pane>
            <el-tab-pane label="密码登录" name="2">
              <el-form :model="userForm" status-icon :rules="userFormRules" ref="userFormRef" label-width="0px">
                <el-form-item prop="email">
                  <el-input v-model="userForm.email" placeholder="请输入邮箱"></el-input>
                </el-form-item>
                <el-form-item prop="password">
                  <el-input type="password" v-model="userForm.password" placeholder="密码" autocomplete="off"></el-input>
                </el-form-item>
                <el-form-item style="text-align: center">
                  <el-button class="btn" type="primary" @click="submitForm('ruleForm')">立即登录</el-button>
                </el-form-item>
              </el-form>
              <div class="register-bar">
                <el-link type="primary" :underline="false" href="https://element.eleme.io" target="_blank">遇到问题？
                </el-link>
                <router-link :to="{ name: 'register'}" style='font-size: 14px;color: #2491f6;'>
                  立即注册
                </router-link>
              </div>
              <div class="tip">
                <p>登录即代表您同意并遵守
                  <a href="">《云N记通行证用户服务协议》</a>
                  <a href="">《云N记通行证用户个人信息及隐私保护政策》</a>
                </p>
              </div>
            </el-tab-pane>
          </el-tabs>
        </el-card>
      </el-col>
      <el-col :span="6"></el-col>
    </el-row>
  </div>
</template>

<script>

export default {
  data () {
    // 验证手机号、邮箱的规则
    var checkEmail = (rule, value, callback) => {
      // const regMobile = /^(0|86|17951)?(13[0-9]|15[0-12356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/
      const regEmail = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/
      // if (regMobile.test(value) || regEmail.test(value)) {
      if (regEmail.test(value)) {
        return callback()
      }
      // 返回一个错误提示
      callback(new Error('请输入合法的邮箱'))
    }
    // 校验密码
    var checkpassword = (rule, value, callback) => {
      const regMobile = /^(?![a-zA-Z]+$)(?![0-9]+$)(?![._~!@#$^&*]+$)(?![0-9a-zA-Z]+$)(?![0-9._~!@#$^&*]+$)(?![a-zA-Z._~!@#$^&*]+$)[A-Za-z0-9._~!@#$^&*]{8,20}$/
      if (regMobile.test(value)) {
        return callback()
      }
      // 返回一个错误提示
      callback(new Error('1.密码必须由字母、数字、特殊符号组成，区分大小写\n' +
        '2.特殊符号包含（. _ ~ ! @ # $ ^ & *）\n' +
        '3.密码长度为8-20位'))
    }

    return {
      activeName: '2',
      userFormRules: {
        email: [{ required: true, message: '请输入邮箱号', trigger: 'blur' },
          { validator: checkEmail, trigger: 'blur' }],
        verificationCode: [{ required: true, message: '请输入验证码', trigger: 'blur' },
          { min: 6, max: 6, message: '验证码错误', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' },
          { validator: checkpassword, trigger: 'blur' }]
      },
      userForm: {
        mobile: '',
        email: '',
        verificationCode: '',
        password: ''
      }
    }
  },
  created () {
  },
  // DOM对象加载完成时
  mounted () {
  },
  methods: {
    submitForm () {
      this.$refs.userFormRef.validate((valid) => {
        if (valid) {
          this.login()
        } else {
          console.log('账号不存在或密码错误！')
          return false
        }
      })
    },
    resetForm () {
      this.$refs.userFormRef.resetFields()
    },
    handleClick (tab, event) {
      // console.log(tab, event)
    },
    async login () {
      // alert(JSON.stringify(this.userForm))
      const url = this.$config.urls.get('user_login')
      const { data: res } = await this.$http.post(url, this.userForm)
      // console.log(res)
      if (res.code === 0) {
        await this.$memory.setToken(res.data.token)
        await this.$memory.setUser(JSON.stringify(res.data.user))
        await this.$router.push({ name: 'index', params: res.data.user })
        return this.$message.success('欢迎回来：' + res.data.user.nick)
      } else {
        return this.$message.error('登录失败:' + res.error_message)
      }
    }
  },
  watch: {
    // 'userForm.mobile' (newValue, oldValue) {
    //   const regMobile = /^(0|86|17951)?(13[0-9]|15[0-12356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/
    //   const regEmail = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/
    //   if (regMobile.test(newValue)) {
    //     console.log('手机号')
    //   } else if (regEmail.test(newValue)) {
    //     this.userForm.email = newValue
    //     this.userForm.mobile = ''
    //     console.log('邮箱')
    //   } else {
    //     console.log('输入错误')
    //   }
    // }
  }
}
</script>

<style lang="less" scoped>
.el-card {
  padding: 20px 60px
}

.el-input, .btn {
  width: 100%;
  margin-top: 15px;
}

.verify {
  width: 60%;
}

.register-bar {
  display: flex;
  text-align: justify;
  color: #2491f6;
  flex-direction: row;
  justify-content: space-between;
  align-content: center;
}

.tip {
  font-size: 12px;

  a {
    color: #2491f6;
  }
}

/deep/ .el-tabs {
  .el-tabs__item {
    font-size: 22px;
    font-weight: bold;
  }
}

.el-tabs__active-bar {
  .is-top {
    width: 156px !important;
  }
}
</style>
